﻿using Core.Domain;
using Core.IRepository;
using Core.Messages;
using Core.Queries;
using Library.Helpers;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Repositories
{   
    public class RoleRepository : IRoleRepository
    {
        private readonly RoleManager<Role> roleManager;
        private readonly IQueryHelper<Role> queryHelper;

        public RoleRepository(AppIdentityDbContext context, RoleManager<Role> roleManager, IQueryHelper<Role> queryHelper)
        {            
            this.roleManager = roleManager;
            this.queryHelper = queryHelper;
            this.queryHelper.Context = context;
        }

        public void Add(Role entity)
        {            
            roleManager.Create(entity);
        }

        public void Delete(Role entity)
        {                        
            roleManager.Delete(entity);
        }

        public void Delete(Expression<Func<Role, bool>> findPredicate)
        {
            throw new NotImplementedException();
        }

        public void Edit(Role entity)
        {
            roleManager.Update(entity);
        }

        public Role FindBy(string id)
        {
            return roleManager.FindById(id);
        }

        public QueryResponse<Role> FindBy(Query<Role> query)
        {            
            return queryHelper.FindBy(roleManager.Roles, query);
        }

        public IList<Role> FindBy(Expression<Func<Role, bool>> findPredicate)
        {
            return roleManager.Roles.Where(findPredicate).ToList();
        }

        public IList<Role> FindAll()
        {
            return roleManager.Roles.ToList();
        }
    }
}